//
//  YSHomeCardVideoView.m
//  Yueshi
//
//  Created by yueshi－mac1 on 15/12/16.
//  Copyright © 2015年 悦食中国. All rights reserved.
//

#import "YSHomeCardVideoView.h"
#import "YSHomeCardHeaderView.h"
#import "YSHomeCardVideoCell.h"
#import "YSHomeCardFooterView.h"
#import "YSVideoDetailsCtrl.h"

@interface YSHomeCardVideoView() <UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic) UITableView *tableView;
@property (strong,nonatomic) NSMutableArray *allFrames;
@property (strong,nonatomic) YSHomeCardHeaderView *headerView;
@property (strong,nonatomic) YSHomeCardFooterView *footerView;
@property (assign,nonatomic) CGFloat allCellHeight;
@property (nonatomic,assign)HomeSubViewSourceType sourceType;

@property (assign, nonatomic) CGFloat superCurrentY ; // 0 表示 不需要头顶部空隙

@end

@implementation YSHomeCardVideoView

-(NSMutableArray *)allFrames{
    if(!_allFrames){
        _allFrames = [NSMutableArray array];
    }
    return _allFrames;
}

-(instancetype)initWithSourceType:(HomeSubViewSourceType)sourceType{
    return [self initWithSourceType:sourceType superCurrentY:1];
}

-(instancetype)initWithSourceType:(HomeSubViewSourceType)sourceType superCurrentY:(CGFloat)superCurrentY{
    self = [super init];
    if(self){
        self.sourceType = sourceType;
        self.superCurrentY = superCurrentY;
        [self setFrame:[UIScreen mainScreen].bounds];
        self.headerView = [YSHomeCardHeaderView headerView];
        self.headerView.superCurrentY = superCurrentY;
        self.footerView = [YSHomeCardFooterView footerView];
        [self initTableView];
        [self setBackgroundColor:PUBLIC_CARD_BG];
    }
    return self;
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]init];
    [self.tableView setFrame:CGRectMake(0, 0, self.width, self.height)];
    [self.tableView setBackgroundColor:PUBLIC_CARD_BG];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setTableHeaderView:self.headerView];
    [self.tableView setTableFooterView:self.footerView];
    [self.tableView setBackgroundColor:PUBLIC_BG];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.scrollEnabled = NO;
    [self addSubview:self.tableView];
}

-(void)setInfo:(YSCardImageResult *)info{
    _info = info;
    [_allFrames addObjectsFromArray: [self itemArrayWitFrames:info.advert]];
    [self.tableView reloadData];
    if (ISBLANK(info.headResult.cardName)) {
        [self.tableView setTableHeaderView:nil];
    }else{
        _allCellHeight += self.superCurrentY ? CARD_HEADERVIEW_HEIGHT: CARD_HEADERVIEW_HEIGHT - 10;
        [self.tableView setTableHeaderView:self.headerView];
        [self.headerView setInfo:info.headResult];
    }
    if(ISBLANK(info.headResult.cardDesc)){
        [self.tableView setTableFooterView:nil];
    }else{
        _allCellHeight += CARD_FOOTERVIEW_HEIGHT;
        [self.tableView setTableFooterView:self.footerView];
        [self.footerView setInfo:info.headResult];
    }
    for (int i = 0 ; i<_allFrames.count; i++) {
        _allCellHeight += [_allFrames[i] cellHeight];
    }
    //根据所有cell的高计算出allCellHeight并设置tableView的高和本身View的高
    [self.tableView setHeight:_allCellHeight];
    [self setHeight:self.tableView.height];
}

/**
 *  根据模型数组 转成 frame模型数据
 */
- (NSArray *)itemArrayWitFrames:(NSArray *)adverts
{
    NSMutableArray *frames = [NSMutableArray array];
    for (YSAdvert *advert in adverts) {
        YSCardVideoItemFrame *frame = [[YSCardVideoItemFrame alloc] init];
        // 传递模型数据，计算所有子控件的frame
        frame.advert = advert;
        [frames addObject:frame];
    }
    return frames;
}

#pragma - mark UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allFrames.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YSHomeCardVideoCell *cell =  [YSHomeCardVideoCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.cardVideoFrame = self.allFrames[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [_allFrames[indexPath.row] cellHeight];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    YSCardVideoItemFrame *frame = self.allFrames[indexPath.row];
//    if(_sourceType == HomeSourceType){
//        [Utils dealUrlWithController:self.superview.viewController url:frame.advert.advertUrl fromWebView:NO];
//    }else{
//        [Utils dealUrlWithController:self.viewController url:frame.advert.advertUrl fromWebView:NO];
//    }
    YSVideoDetailsCtrl *ctrl = [[YSVideoDetailsCtrl alloc] initWithAdvert:self.info.advert[indexPath.row]];
    [self.superview.viewController.navigationController pushViewController:ctrl animated:YES];
}

@end
